package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.bean.Category;
import com.chen.bean.Dish;
import com.chen.bean.DishFlavor;
import com.chen.common.R;
import com.chen.dto.DishDto;
import com.chen.exception.CustomException;
import com.chen.mapper.DishDao;
import com.chen.service.CategoryService;
import com.chen.service.DishFlavorService;
import com.chen.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     *
     * @param dishDto
     * @return
     */
    @Override
    public R<String> addDish(DishDto dishDto) {
        //判断菜品名称在菜品表中是否存在
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getName, dishDto.getName());
        int count = this.count(wrapper);
        if (count > 0) {
            throw new CustomException("菜品名称" + dishDto.getName() + "已存在");
        }
        //存菜品信息
        boolean flag = this.save(dishDto);
        //存储菜品口味信息取口味表  关联菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(flavor -> {
            flavor.setDishId(dishDto.getId());
            dishFlavorService.save(flavor);
        });
        return R.success("新增成功");
    }

    /**
     * 菜品分页展示
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R<Page<DishDto>> dishPage(Integer page, Integer pageSize, String name) {
        Page<Dish> Page = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null && name.length() != 0, Dish::getName, name);

        Page<Dish> dishPage = this.page(Page, wrapper);

        Page<DishDto> dishDtoPage = new Page<>();
        //复制dishpage属性值到dishdtopage上
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");

        List<Dish> dishList = dishPage.getRecords();
        List<DishDto> dishDtoList = new ArrayList<>();
        dishList.stream().forEach(dish -> {
            //将dish的数据封装到dishDto中
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //查询获取菜品分类名称 封装到dishDto中
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category == null ? "未知类型" : category.getName());
            //将dishDto存入dishDtoPage中
            dishDtoList.add(dishDto);
        });
        dishDtoPage.setRecords(dishDtoList);
        return R.success(dishDtoPage);
    }

    /**
     * 回显要修改的菜品信息
     *
     * @param id
     * @return
     */
    @Override
    public R<DishDto> echoDish(Long id) {
        //创建dishdto对象  封装响应数据
        DishDto dishDto = new DishDto();

        Dish dish = this.getById(id);
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(wrapper);
        dishDto.setFlavors(dishFlavorList);
        return R.success(dishDto);
    }

    /**
     * 修改菜品信息
     *
     * @param dishDto
     * @return
     */
    @Override
    public R<String> updateDish(DishDto dishDto) {
        LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Dish::getName, dishDto.getName());
        wrapper1.ne(Dish::getId, dishDto.getId());
        int count = this.count(wrapper1);
        if (count > 0) {
            throw new CustomException("修改的菜品名称" + dishDto.getName() + "已存在");
        }
        //修改菜品信息
        this.updateById(dishDto);
        //修改菜品中的口味信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dishDto.getId());
        //先删除
        dishFlavorService.remove(wrapper);
        //在保存新的
        List<DishFlavor> dishDtoFlavors = dishDto.getFlavors();
        dishDtoFlavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavorService.save(dishFlavor);
        });
        return R.success("修改菜品成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public R<String> deleteDish(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new CustomException("请先勾选后在删除");
        }
        //删除菜品信息
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        this.remove(wrapper);
        //删除菜品关联口味信息
        LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(wrapper1);
        return R.success("删除成功");
    }

    /**
     * 修改状态
     *
     * @param status
     * @param ids
     * @return
     */
    @Override
    public R<String> updateStatus(Integer status, Long[] ids) {
        if (ids == null || ids.length == 0)
            throw new CustomException("请选择后在修改");

        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        Dish dish = new Dish();
        dish.setStatus(status);
        this.update(dish, wrapper);

        return R.success("修改成功");
    }

    /**
     * 按菜品分类id  和菜品名字查询
     * @param dish
     * @return
     */
    @Override
    public R<List<Dish>> findByCategoryIdOrName(Dish dish) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        wrapper.like(dish.getName()!=null,Dish::getName,dish.getName());
        wrapper.eq(Dish::getStatus,1);
        List<Dish> list = this.list(wrapper);
        return R.success(list);
    }
}
